package com.wardrobe.soa.backend.goods_classify;

import com.google.common.collect.Maps;
import com.wardrobe.domain.banner.BannerBean;
import com.wardrobe.domain.banner.BannerShowBean;
import com.wardrobe.domain.common.BackEndFromBean;
import com.wardrobe.domain.goods_classify.GoodsClassifyBean;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2018/11/16.
 */
@RestController
@RequestMapping("/backend/goodsclassify")
public class BackGoodsClassifyController {
    private static final org.slf4j.Logger logger = LoggerFactory.getLogger(BackGoodsClassifyController.class);

    @Autowired
    private BackGoodsClassifyService backGoodsClassifyService;

    @RequestMapping("/goodsClassifyList")
    public Map<String, Object> goodsClassifyList(int status) {
        Map<String, Object> result = Maps.newHashMap();
        List<GoodsClassifyBean> list = backGoodsClassifyService.getGoodsClassifyList(status);
        list.stream().forEach(goodsClassifyBean -> {
            goodsClassifyBean.setCreateTime(goodsClassifyBean.getCreateTime().substring(0,goodsClassifyBean.getCreateTime().length()-2));
            goodsClassifyBean.setUpdateTime(goodsClassifyBean.getUpdateTime().substring(0,goodsClassifyBean.getUpdateTime().length()-2));
        });
        result.put("list", list);
        return result;
    }

    @RequestMapping("/classifyList")
    public List<Map<String, Object>>  classifyList(int status) {
       return backGoodsClassifyService.classifyList(status);
    }

    @RequestMapping("/saveGoodsClassify")
    public Map<String,Object> saveGoodsClassify(GoodsClassifyBean goodsClassifyBean){
        Map<String,Object> result = Maps.newHashMap();
        try {
            int status = backGoodsClassifyService.saveGoodsClassify(goodsClassifyBean);
            if(status==1){
                result.put("status",status);
                result.put("msg","添加成功");
            }else{
                result.put("status",2);
                result.put("msg","添加失败");
            }
            return result;
        }catch (Exception e){
            result.put("status",2);
            result.put("msg","添加失败");
            logger.error("saveGoodsClassify error",e);
        }
        return result;
    }


    @RequestMapping("/modifyGoodsClassify")
    public Map<String,Object> modifyGoodsClassify(BackEndFromBean fromBean){
        Map<String,Object> result = Maps.newHashMap();
        try {
            fromBean.setUpdateTime(new Date());
            int status = backGoodsClassifyService.modifyGoodsClassify(fromBean);
            if(status==1){
                result.put("status",status);
                result.put("msg","修改成功");
            }else{
                result.put("status",2);
                result.put("msg","修改失败");
            }
            return result;
        }catch (Exception e){
            result.put("status",2);
            result.put("msg","修改失败");
            logger.error("modifyGoodsClassify error",e);
        }
        return result;
    }
}
